package sjtu.ist.warehouse4ws.action;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.StringBufferInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.ServletActionContext;
import org.xml.sax.SAXException;

import sjtu.ist.warehouse4ws.epc2bpmn.ParserEpc;
import sjtu.ist.warehouse4ws.jcr.RepositoryMoudl;
import sjtu.ist.warehouse4ws.tool.FileIO;
import sjtu.ist.warehouse4ws.tool.GenerateWsdl;
import sjtu.ist.warehouse4ws.tool.MyUtils;

import com.opensymphony.xwork2.ActionSupport;

public class GenerateWsdlAction  extends ActionSupport{
	private GenerateWsdl generateWsdl;
	String warnning;
	public void setGenerateWsdl(GenerateWsdl generateWsdl) {
		this.generateWsdl = generateWsdl;
	}

	public String getWarnning() {
		return warnning;
	}
	public void setWarnning(String warnning) {
		this.warnning = warnning;
	}
	private RepositoryMoudl repositoryMoudlService;
    
	
	public RepositoryMoudl getRepositoryMoudlService() {
		return repositoryMoudlService;
	}

	public void setRepositoryMoudlService(RepositoryMoudl repositoryMoudlService) {
		this.repositoryMoudlService = repositoryMoudlService;
	}
	public String gernerateWsdl() throws Exception {

	
		MyUtils.mkDirectory(ServletActionContext.getServletContext().getRealPath("/upload/files/wsdl"));
		
		//basepath is dynamic
		String outpath=ServletActionContext.getServletContext().getRealPath("/upload/files/wsdl/").replaceAll("\\\\", "/")+"/";
	   
		
	
		Subject currentUser = SecurityUtils.getSubject();
		String companyId=currentUser.getSession().getAttribute("companyId").toString();	

		HttpServletRequest request = ServletActionContext.getRequest();
		String xmlName = request.getParameter("nodeName");
		String xmlVersion=request.getParameter("nodeVersion");
		
		
		  HashMap <String,String>fhm=this.repositoryMoudlService.getAllFile(companyId, xmlName, xmlVersion,"function");
		  HashMap <String,String>dhm=this.repositoryMoudlService.getAllFile(companyId, xmlName, xmlVersion,"domain");
		  List<String> addFile=new ArrayList<String>();
		  generateWsdl.setDomainHM(dhm);
		  generateWsdl.setFunctionHM(fhm);
		    Iterator it = fhm.keySet().iterator();
			while (it.hasNext()) {
				
				String key = (String) it.next();
				String outFile=outpath+"WSDL_"+key;
				try {
					generateWsdl.writeWsdl(outFile, key);
					addFile.add(outFile);
					
					warnning="生成WSDL文件成功！";
				} catch (SAXException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					warnning="关联文件缺失，请添加相关文件！";
		
				}catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					warnning="关联文件缺失，请添加相关文件！";
		
				}
						
		
					
			}
			this.repositoryMoudlService.addServiceFile(companyId, xmlName, xmlVersion, "wsdl", addFile);
			
				

		return SUCCESS;

		
		
	}

}
